<?php

class Application_Model_DbTable_UniqueTokens extends Zend_Db_Table_Abstract
{

    protected $_name = 'unique_tokens';
    protected $_dependentTables = array('Application_Model_DbTable_Users');

    public function addUniqueToken($token, $url, $username)
    {
    	$currentDate = date('Y-m-d H:i:s', time());
    	$data = array(
    			'date_created' => $currentDate,
    			'date_updated' => $currentDate,
    			'token' => $token,
    			'generated_url' => $url,
    			'generated_by' => $username,
    	);
    	return $this->insert($data);
    }
    
    public function getUniqueToken($id)
    {
    	$id = (int) $id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		throw new Exception("Could not find row $id");
    	
    	}
    	return $row->toArray();
    }
    
    public function getUniqueTokenByToken($token)
    {
    	$row = $this->fetchRow('token = \'' . $token . '\'');
    	
    	if (!$row) {
    		throw new Exception("Could not find row with token: $token");
    	}
    	
    	return $row->toArray();
    }
    
    public function updateUniqueToken($id, $data)
    {
    	$currentDate = date('Y-m-d H:i:s', time());
    	$data['date_updated'] = $currentDate; 
    	$this->update($data, 'id = '. (int) $id);
    }
    
    public function deleteUniqueToken($id)
    {
    	$this->delete('id = ' . (int) $id);
    }
}

